﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MyChart
{
    /// <summary>
    /// 表示图表控件的单序列图表数据项集合
    /// </summary>
    public class SingleSeriesManager : ItemManager
    {
        #region 字段

        private ChartItemCollection _items = new ChartItemCollection();

        #endregion

        #region 属性

        /// <summary>
        /// 获取数据项集合
        /// </summary>
        public ChartItemCollection Items
        {
            get { return _items; }
        }

        /// <summary>
        /// 获取当前数据集合是否为空
        /// </summary>
        public override bool IsEmpty
        {
            get {
                if (_items.Count == 0)
                {
                    return true;
                }
                else
                {
                    foreach (ChartItem item in _items)
                    {
                        double tmp;
                        if (double.TryParse(item.Value, out tmp))
                        {
                            if (tmp != 0)
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }

                    return true;
                }
            }
        }

        #endregion

        #region 构造器

        /// <summary>
        /// 初始化 SingleSeriesManager 类
        /// </summary>
        /// <param name="chart">关联数据项集合的控件实例</param>
        public SingleSeriesManager(FusionChart chart)
            : base(chart)
        {

        }

        #endregion

        #region 方法

        /// <summary>
        /// 获取当前集合对应的 FusionChart Xml配置字符串
        /// </summary>
        /// <returns>Xml配置字符串</returns>
        public override string GetXmlString()
        {
            StringBuilder xml = new StringBuilder();

            int iItemIndex = 0;
            base.DictArguments = new Dictionary<int, Dictionary<int, List<string>>>();

            double dItemValue = 0;
            foreach (ChartItem item in Items)
            {
                if (string.IsNullOrEmpty(item.Value))   //这里判断如果值为空或者null，那么更加业务需求，后续的item应该是不存在的，而不是自动填充0
                {
                    break;
                }

                string toolText = "";
                bool hasToolText = GetFormattedToolText(item.Row, out toolText);

                string scriptText = string.Empty;
                bool hasScriptText = GetFormattedScriptText(item.Row, 0, iItemIndex, out scriptText);

                double.TryParse(item.Value, out dItemValue);

                xml.Append("<set" + (hasScriptText ? " link='" + scriptText + "'" : "") + (hasToolText ? " toolText='" + toolText + (Chart.ShowPercentInToolTip ? @"\n    比例：" + (dItemValue * 100).ToString() + "%" : "") + "'" : "")
                    + " label='" + item.Text.Replace("'", "").Replace("\"", "")
                    + "' value='" + (Chart.NumberSuffix == "%" ? (dItemValue * 100).ToString("F" + this.Chart.Decimals) : dItemValue.ToString("F" + this.Chart.Decimals)) + "'"
                    + " displayValue='" + (Chart.NumberSuffix == "%" ? (dItemValue * 100).ToString("F" + this.Chart.Decimals) + "%" : dItemValue.ToString("F" + this.Chart.Decimals)) + "'"
                    + (item.Color.ToArgb() != 0 ? " color='" + item.Color.ToArgb().ToString("X").Remove(0, 2) + "'" : "") + " />");
                iItemIndex++;
            }
            return xml.ToString();
        }

        #endregion
    }
}
